import * as types from "./mutation-types";
import Api from "api/api";

const userInfo = window.sessionStorage.getItem("userInfo");

const state = {
  userList: {},
  //职位列表
  jobList: {},
  //不包含全部部门列表
  deptNotAllList: [],
  //党员身份
  userTypeList: {},
  currentUser: {},
  currentTokenUser: userInfo ? JSON.parse(userInfo) : {},
  userNotList: {},
  userView: {},
  //用户编辑获取数据
  userInfo: {},
  userDetail: {},
  userDetailUpdate: {},
  copyUser: {},
  contactUserInfo: {}
};

const getters = {
  jobList: state => state.jobList,
  deptNotAllList: state => state.deptNotAllList,
  userTypeList: state => state.userTypeList,
  userView: state => state.userView,
  userList: state => state.userList,
  currentUser: state => state.currentUser,
  currentTokenUser: state => state.currentTokenUser,
  userNotList: state => state.userNotList,
  //用户编辑获取数据
  userInfo: state => state.userInfo,
  userDetail: state => state.userDetail,
  userDetailUpdate: state => state.userDetailUpdate,
  copyUser: state => state.copyUser
};

const actions = {
  //通讯录复制用户到另一个部门
  contactsDeptCopy({ commit }, param) {
    return Api.copy(param).then(ret => {
      return ret;
    });
  },
  //复制用户到另一个部门
  copyUser({ commit }, param) {
    return Api.copyUser(param).then(ret => {
      commit(types.COPY_USER, ret.data);
      return ret;
    });
  },
  //查询用户详情
  getUserDetail({ commit }, param) {
    return Api.getUserByUserId(param).then(ret => {
      commit(types.UPDATE_USER_DETAIL, ret.data);
      return ret;
    });
  },
  //修改密码
  changePassword({ commit }, param) {
    return Api.changePassword(param).then(ret => {
      return ret;
    });
  },
  //部门列表（不包含所有部门）
  getOrganizeTreeGETS({ commit }, param) {
    return Api.getOrganizeTree(param).then(ret => {
      commit(types.DEPT_NOT_ALL_LIST, ret.data);
      return ret;
    });
  },
  //查看字典选项
  viewDictUsingPOSTS({ commit }, param) {
    return Api.viewGroupDictUsingGET(param).then(ret => {
      commit(types.JOB_USER_LIST, ret.data);
      return ret;
    });
  },
  //用户列表
  userListUsingPOSTSOrg({ commit }, param) {
    return Api.getUserPageList(param).then(ret => {
      commit(types.UPDATE_USER_LIST, ret.data);
      return ret;
    });
  },
  userNotListUsingPOSTS({ commit }, param) {
    return Api.roleUserNotListUsingPOST(param).then(ret => {
      commit(types.UPDATE_USER_NOT_LIST, ret.data);
      return ret;
    });
  },
  //新增修改用户
  userSaveUsingPOSTS({ commit }, payload) {
    return Api.addOrUpdateUser(payload).then(ret => {
      commit(types.DEPT_VIEW_USER, ret.data);
      return ret;
    });
  },
   //通讯录中新增用户
   contactSaveUsingPOSTS({ commit }, payload) {
    return Api.addOrUpdateContacts(payload).then(ret => {
      commit(types.UPDATE_USER_CONINIT, ret.data);
      return ret;
    });
  },
    //通讯录中修改用户
    contactDeleteUsingPOSTS({ commit }, payload) {
      return Api.UpdateContacts(payload).then(ret => {
        commit(types.UPDATE_USER_CONINIT, ret.data);
        return ret;
      });
    },
      //删除用户
  userDelContactUsingDELETES({ commit }, payload) {
    return Api.deleteContactsById(payload).then(ret => {
      return ret;
    });
  },
  // //修改用户
  // userViewEditS({ commit }, param) {
  //   commit(types.DEPT_VIEW_USER, param);
  //   return param;
  // },
  //删除用户
  userDelUsingDELETES({ commit }, payload) {
    return Api.deleteUserByUserId(payload).then(ret => {
      return ret;
    });
  },
  //用户分配角色
  grantRoleUsingPOSTS({ commit }, payload) {
    return Api.grantRole(payload).then(ret => {
      return ret;
    });
  },
  // 新增或修改角色用户关联
  addOrUpdateRoleUserS({ commit }, payload) {
    return Api.addOrUpdateRoleUser(payload).then(ret => {
      return ret;
    });
  },

  //token获取当前用户详情,包含部门,角色信息
  userInfoUsingPOSTS({ commit }, param) {
    return Api.getUserDetail(param).then(ret => {
      commit(types.UPDATE_CURRENT_TOKEN_USER, ret.data);
      return ret;
    });
  },
  //用户查询 党员导出
  userExcelUsingGETS({ commit }, payload) {
    return Api.userExcelUsingGET(payload).then(ret => {
      return ret;
    });
  },

  //钉钉用户详情,包含部门,角色信息

  //编辑 获取详情
  userViewUsingPOSTS({ commit }, param) {
    return Api.userViewUsingPOST(param).then(ret => {
      commit(types.UPDATE_CURRENT_USER, ret.data);
      return ret;
    });
  },
  //用户编辑获取数据
  userUserEditS({ commit }, param) {
    commit(types.DEPT_USER, param);
    return param;
  },
  //删除角色
  DelRoleUsingPOSTS({ commit }, param) {
    return Api.deleteRoleUserById(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  //JOB_USER_LIST
  [types.JOB_USER_LIST](state, payload) {
    state.jobList = payload;
  },
  [types.COPY_USER](state, payload) {
    state.copyUser = payload;
  },
  [types.DEPT_NOT_ALL_LIST](state, payload) {
    state.deptNotAllList = payload;
  },
  [types.UPDATE_USER_LIST](state, payload) {
    state.userList = payload;
  },

  [types.UPDATE_USER_NOT_LIST](state, payload) {
    state.userNotList = payload;
  },
  [types.UPDATE_CURRENT_USER](state, payload) {
    state.currentUser = payload;
  },
  //用户编辑获取数据
  [types.DEPT_USER](state, payload) {
    state.userInfo = payload;
  },

  [types.UPDATE_CURRENT_TOKEN_USER](state, payload) {
    state.currentTokenUser = payload;
    window.sessionStorage.setItem("userInfo", JSON.stringify(payload));
  },
  [types.UPDATE_USER_DETAIL](state, payload) {
    state.userDetail = payload;
  },
  [types.DEPT_VIEW_USER](state, payload) {
    state.userDetailUpdate = payload;
  },
  [types.UPDATE_USER_CONINIT](state, payload) {
    state.contactUserInfo = payload;
  },

  // [types.UPDATE_USER_INIT](state, payload) {
  //   state.userView = {
  //     deptId: "",
  //     ehrNo: "",
  //     enterAt: "",
  //     mobile: "",
  //     name: "",
  //     password: "",
  //     position: "",
  //     roleId: "",
  //     sex: "",
  //     userId: "",
  //     userType: ""
  //   };
  // },
  //初始化新增数据
  [types.UPDATE_USER_INIT](state, payload) {
    state.userInfo = {
      deptId: "",
      deptName: "",
      dingId: "",
      ehrNo: "",
      enterAt: "",
      idNumber: "",
      mobile: "",
      name: "",
      position: "",
      password: "",
      positionName: "",
      roleId: "",
      roleName: "",
      sex: "",
      userId: "",
      userType: "",
      userTypeName: ""
    };
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
